package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/*套餐*/
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    //@Autowired
    // private JedisPool jedisPool;

    //    文件上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {

        try {
            String originalFilename = imgFile.getOriginalFilename();//获取原始文件名
            int index = originalFilename.lastIndexOf(".");//获取后缀名
            String extention = originalFilename.substring(index - 1);
            //java自带产生的随机数拼接字符串
            String fileName = UUID.randomUUID().toString() + extention;
            /*使用七牛云工具类*/
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);
            Result result = new Result(true, "成功", fileName);
            result.setData(fileName);
            //将上传图片名称存入Redis，基于Redis的Set集合存储
            //  jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false);
        }
    }

    //添加套餐
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, @RequestParam("checkgroupIds") Integer[] checkgroupIds) {
        try {
            setmealService.add(setmeal, checkgroupIds);
            return new Result();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false);
        }
    }

    //分页
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/download")
    public Result download(@RequestBody List<Setmeal> dataList) {


        return new Result(false);
    }

    @RequestMapping("/power")
    public Result power(int id, Boolean open) {
        try {
            setmealService.power(id, open);
            return new Result();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false);
        }

    }
}
